package flc.ast.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.stark.picselect.config.PhotoConfig;
import com.stark.picselect.entity.SelectMediaEntity;
import com.stark.picselect.utils.MediaLoadManager;

import java.util.ArrayList;
import java.util.List;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.adapter.SelPicShowAdapter;
import flc.ast.adapter.SelPictureAdapter;
import flc.ast.bean.SelPicShowBean;
import flc.ast.databinding.ActivitySelPicBinding;
import flc.ast.databinding.ActivitySelPictureBinding;
import io.reactivex.rxjava3.core.ObservableEmitter;
import stark.common.basic.utils.RxUtil;
import stark.common.basic.utils.StkPermissionHelper;

public class SelPictureActivity extends BaseAc<ActivitySelPictureBinding> {

    private SelPictureAdapter pictureAdapter;
    public static List<SelPicShowBean> listBottom = new ArrayList<>();
    private SelPicShowAdapter showAdapter;

    @Override
    protected int onCreate() {
        return R.layout.activity_sel_picture;
    }

    @Override
    protected void initView() {
        mDataBinding.ivSelPictureBack.setOnClickListener(v -> onBackPressed());
        mDataBinding.ivImport.setOnClickListener(this);
        //相册图片
        pictureAdapter = new SelPictureAdapter();
        mDataBinding.rvSelPictureList.setLayoutManager(new GridLayoutManager(mContext, 3));
        mDataBinding.rvSelPictureList.setAdapter(pictureAdapter);
        pictureAdapter.setOnItemClickListener(this);
        //底部选中图片
        showAdapter = new SelPicShowAdapter();
        mDataBinding.rvSelPictureShowList.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.HORIZONTAL, false));
        mDataBinding.rvSelPictureShowList.setAdapter(showAdapter);
        showAdapter.addChildClickViewIds(R.id.ivSelPicShowItemCha);
        showAdapter.setOnItemChildClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.ivImport://导入
                List<String> listPath = new ArrayList<>();
                for (SelPicShowBean selPicShowBean : listBottom) {
                    if (!TextUtils.isEmpty(selPicShowBean.getPath())) {
                        listPath.add(selPicShowBean.getPath());
                    }
                }
                if (listPath.size() == 0) {
                    ToastUtils.showShort(getString(R.string.please_sel_picture));
                    return;
                }
                MusicAlbumActivity.listPath=listPath;
                startActivity(MusicAlbumActivity.class);
                break;
        }
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        if (pictureAdapter.getItem(position).isChecked()) {
            pictureAdapter.getItem(position).setChecked(false);
            deleteShow(pictureAdapter.getItem(position).getPath());
        } else {
            addShow(pictureAdapter.getItem(position).getPath(), position);
        }
    }

    @Override
    public void onItemChildClick(@NonNull BaseQuickAdapter adapter, @NonNull View view, int position) {
        switch (view.getId()) {
            case R.id.ivSelPicShowItemCha:
                if (!TextUtils.isEmpty(showAdapter.getItem(position).getPath())) {
                    String path = showAdapter.getItem(position).getPath();
                    for (int i = 0; i < pictureAdapter.getValidData().size(); i++) {
                        if (path.equals(pictureAdapter.getItem(i).getPath())) {
                            pictureAdapter.getItem(i).setChecked(false);
                            pictureAdapter.notifyItemChanged(i);
                        }
                    }
                    deleteShow(path);
                }
        }
    }

    @Override
    protected void initData() {
        getPermission();
        //准备底部数据
        listBottom.clear();
        listBottom.add(new SelPicShowBean("", 1, false));
        listBottom.add(new SelPicShowBean("", 2, false));
        listBottom.add(new SelPicShowBean("", 3, false));
        listBottom.add(new SelPicShowBean("", 4, false));
        listBottom.add(new SelPicShowBean("", 5, false));
        listBottom.add(new SelPicShowBean("", 6, false));
        listBottom.add(new SelPicShowBean("", 7, false));
        listBottom.add(new SelPicShowBean("", 8, false));
        listBottom.add(new SelPicShowBean("", 9, false));
        showAdapter.setList(listBottom);
    }

    private void getPermission() {
        StkPermissionHelper.permission(StkPermissionHelper.Permission.ACCESS_IMAGE_VIDEO)
                .reqPermissionDesc(getString(R.string.get_picture_permission))
                .callback(new StkPermissionHelper.ACallback() {
                    @Override
                    public void onGranted() {
                        getData();
                    }

                    @Override
                    public void onDenied(boolean needGoSettings) {
                        mDataBinding.rvSelPictureList.setVisibility(View.GONE);
                        mDataBinding.tvSelPicNoData.setVisibility(View.VISIBLE);
                    }
                }).request();
    }

    //获取视频和图片数据
    private void getData() {
        RxUtil.create(new RxUtil.Callback<List<SelectMediaEntity>>() {
            @Override
            public void doBackground(ObservableEmitter<List<SelectMediaEntity>> emitter) {
                List<SelectMediaEntity> listSys = MediaLoadManager.loadAllMediaData(mContext, PhotoConfig.Mode.PHOTO);
                emitter.onNext(listSys);
            }

            @Override
            public void accept(List<SelectMediaEntity> selectMediaEntities) {
                if (selectMediaEntities.size() > 0) {
                    pictureAdapter.setList(selectMediaEntities);
                    mDataBinding.rvSelPictureList.setVisibility(View.VISIBLE);
                    mDataBinding.tvSelPicNoData.setVisibility(View.GONE);
                } else {
                    mDataBinding.rvSelPictureList.setVisibility(View.GONE);
                    mDataBinding.tvSelPicNoData.setVisibility(View.VISIBLE);
                }
            }
        });
    }

    //添加展示
    private void addShow(String path, int position) {
        //路径不为空的数量
        int num = 0;
        for (SelPicShowBean selPicShowBean : listBottom) {
            if (!TextUtils.isEmpty(selPicShowBean.getPath())) {
                num++;
            }
        }
        if (num >= 9) {
            ToastUtils.showShort(getString(R.string.sel_pic_tip));
            return;
        }
        pictureAdapter.getItem(position).setChecked(true);
        pictureAdapter.notifyDataSetChanged();
        listBottom.get(num).setPath(path);
        listBottom.get(num).setSelect(false);
        if (num < 8) {
            listBottom.get(num + 1).setSelect(true);
        }
        showAdapter.setList(listBottom);
    }

    //删除展示
    private void deleteShow(String path) {
        List<String> list = new ArrayList<>();
        for (SelPicShowBean selPicShowBean : listBottom) {
            if (!TextUtils.isEmpty(selPicShowBean.getPath())) {
                list.add(selPicShowBean.getPath());
            }
        }
        //删除选择的图片路径
        list.remove(path);
        for (SelPicShowBean selPicShowBean : listBottom) {
            selPicShowBean.setPath("");
            selPicShowBean.setSelect(false);
        }
        for (int i = 0; i < list.size(); i++) {
            listBottom.get(i).setPath(list.get(i));
        }
        pictureAdapter.notifyDataSetChanged();
        listBottom.get(list.size()).setSelect(true);
        showAdapter.setList(listBottom);
    }

}